import hitrace from '@ohos.hiTraceMeter';
import systemDateTime from '@ohos.systemDateTime';

export class TransformerTracer {
  private static readonly TRACE_ID = 1;

  static traceSync<R>(name: string, block: () => R): R {
    hitrace.startTrace(name, TransformerTracer.TRACE_ID);
    const result = block();
    hitrace.finishTrace(name, TransformerTracer.TRACE_ID);
    return result;
  }

  static dumpCost<R>(name: string, block: () => R): R {
    const startTime = systemDateTime.getTime(false);
    const result = block();
    const timeCost = systemDateTime.getTime(false) - startTime;
    console.info(`[TransformerTracer] ${name} cost: ${timeCost}ms`);
    return result;
  }
}
